﻿using System;
using System.Data;
using System.Collections.Generic;
using Clipper.Model;
using System.Collections.ObjectModel;
using Clipper.BLL;
using Clipper.DAL;
using Global;
namespace Clipper.BLL
{
    /// <summary>
    /// DetectItems
    /// </summary>
    public partial class DetectItems
    {
        private readonly Clipper.DAL.DetectItems dal = new Clipper.DAL.DetectItems();
        DAL.DetectVariety mdetectVarietyDal = new DAL.DetectVariety();
        DAL.FittingApproachInfo mfittingApproachInfoDal = new DAL.FittingApproachInfo();
        DAL.CoordinateInfo mcoordinateInfoDal = new DAL.CoordinateInfo();
        DAL.StandardState mstandardStateDal = new DAL.StandardState();

        public DetectItems()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string detectItem)
        {
            return dal.Exists(detectItem);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Clipper.Model.DetectItems model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Clipper.Model.DetectItems model)
        {
            return dal.Update(model);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string detectItem)
        {
    
            return dal.Delete(detectItem);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string detectItemlist)
        {
            return dal.DeleteList(detectItemlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Clipper.Model.DetectItems GetModel(string detectItem)
        {

            return dal.GetModel(detectItem);
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public ObservableCollection<Clipper.Model.DetectItems> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public ObservableCollection<Clipper.Model.DetectItems> DataTableToList(DataTable dt)
        {
            ObservableCollection<Clipper.Model.DetectItems> modelList = new ObservableCollection<Clipper.Model.DetectItems>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Clipper.Model.DetectItems model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        model.DetectVariety = mdetectVarietyDal.GetModel(model.DetectVarietyID);
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod

        #region  ExtensionMethod

         ///<summary>
         ///获得数据列表完整的检测项信息,用于表格显示
         ///</summary>
        public ObservableCollection<Clipper.Model.DetectItems> GetFullDetectItemsInfoList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            ObservableCollection<Clipper.Model.DetectItems> modelList = new ObservableCollection<Clipper.Model.DetectItems>();
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                Clipper.Model.DetectItems model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(ds.Tables[0].Rows[n]);
                    if (model != null)
                    {
                        model.DetectVariety = mdetectVarietyDal.GetModel(model.DetectVarietyID);
                        model.FittingApproachInfo = mfittingApproachInfoDal.GetModel(model.FittingApproachID);
                        model.coordinateInfo = mcoordinateInfoDal.GetModel(model.CoordinateID);
                        model.StandardStateModel = mstandardStateDal.GetModel(model.standardState);
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        ///<summary>
        ///获得检测项选择列表
        ///</summary>
        public ObservableCollection<Clipper.Model.DetectItems> GetSelectItemsList()
        {
            DataSet ds = dal.GetSelectList();
            ObservableCollection<Clipper.Model.DetectItems> modelList = new ObservableCollection<Clipper.Model.DetectItems>();
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                Clipper.Model.DetectItems model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(ds.Tables[0].Rows[n]);
                    if (model != null)
                    {
                        model.DetectVariety = mdetectVarietyDal.GetModel(model.DetectVarietyID);
                        model.FittingApproachInfo = mfittingApproachInfoDal.GetModel(model.FittingApproachID);
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        /// <summary>
        /// 获得可以在结果对照中添加的数据 
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<Model.DetectItems> GetResultReferenceList()
        {
            DataSet ds = dal.GetList("detectItem not in(select detectItem from resultReference)");
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得可以再质控对照表中添加的数据
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<Model.DetectItems> GetQCReferenceList()
        {
            DataSet ds = dal.GetList("detectItem not in(select detectItem from QCReference)");
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="password"></param>
        public bool Export(string fileName,string password)
        {
            return dal.ExportExcel(fileName, password,ServiceProvider.FileVersion);
        }
        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="password">密码</param>
        /// <param name="version">数据模板版本</param>
        public bool? Import(string fileName, string password)
        {
            bool? res = false;
           
            //if (SQLiteHelper.OpenDatabase(fileName, password))
            //{
            //    dal.Import(fileName, password);
            //    res = true;
            //}
            //else
            //{
            //    res = false;//密码不正确或文件不正确
            //}
            if (ExcelHelper.CheckFileVersion(fileName, password, password, ServiceProvider.FileVersion))
            {
                res= dal.ImportExcel(fileName, password);
            }
            else
            {
                res = null;//版本不匹配
            }
            return res;
        }
        #endregion  ExtensionMethod
    }
}


